////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CButton.cpp
//
//		Author			:		Jayson Wu
//
//		Purpose			:		Functions and members of the CButton class
//
//		Dates			:		Written( April 5, 2011 ) by Jayson Wu
//	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CButton.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../Managers/VIN_ObjectManager.h"
#include "../Visual/VIN_BitmapFont.h"
#include "../Game Objects/CMasterHand.h"
#include "../Core/MessageSystem.h"
#include "../Managers/InputManager.h"
#include "../Managers/vin_soundManager.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../Game States/CGamePlayState.h"

CButton::CButton()
{
	SetType(OBJ_BUTTON);
	SetGroundPt(99);

	timer = 0.1f;
	CoolDownMax = 10;
	CurrCoolDown = 10;
}

CButton::~CButton()
{

}

void CButton::Render()
{
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	VIN_BitMapFont* pBF = VIN_BitMapFont::GetInstance();
	int Bars = MessageSystem::GetInstance()->GetStaticImageID(IMAGE_PIXEL);

	int ButtonLevel = 1;

	//RockButton
	if(GetImageID() != -1)
		pTM->Draw(GetImageID(), ButtonRect.left, ButtonRect.top, 
		(float)((float)(ButtonRect.right - ButtonRect.left)/(float)pTM->GetTextureWidth(GetImageID())), 
		(float)((float)(ButtonRect.bottom - ButtonRect.top)/(float)pTM->GetTextureHeight(GetImageID())),
		0,0,0,0, D3DCOLOR_ARGB(ButtonColor.A, ButtonColor.R, ButtonColor.G,ButtonColor.B));
	if(ItemID != -1)
		pTM->Draw(ItemID,ButtonRect.left + 10, ButtonRect.top + 10, ItemSize, ItemSize);

	if(GetButtonType() == BUTTON_HAND)
	{
		CurrCoolDown = CGamePlayState::GetInstance()->GetHand()->GetPinchTimer();
		CoolDownMax = CGamePlayState::GetInstance()->GetHand()->GetPinchTimerMax();
		ButtonLevel = CGamePlayState::GetInstance()->GetHand()->GetLevel();
	}
	else if(GetButtonType() == BUTTON_ROCK)
	{
		CurrCoolDown = CGamePlayState::GetInstance()->GetHand()->GetRockCoolDown();
		CoolDownMax = CGamePlayState::GetInstance()->GetHand()->GetRockCoolDownMax();
		ButtonLevel = CGamePlayState::GetInstance()->GetHand()->GetRockLevel();
	}
	else if(GetButtonType() == BUTTON_POISON)
	{
		CurrCoolDown = CGamePlayState::GetInstance()->GetHand()->GetPoisonCoolDown();
		CoolDownMax = CGamePlayState::GetInstance()->GetHand()->GetPoisonCoolDownMax();
		ButtonLevel = CGamePlayState::GetInstance()->GetHand()->GetLevel();
	}
	else if(GetButtonType() == BUTTON_BALLISTA)
	{
		CurrCoolDown = CGamePlayState::GetInstance()->GetHand()->GetBallistaCoolDown();
		CoolDownMax = CGamePlayState::GetInstance()->GetHand()->GetBallistaCoolDownMax();
		ButtonLevel = 9001;
	}
	else if(GetButtonType() == BUTTON_OIL)
	{
		CurrCoolDown = CGamePlayState::GetInstance()->GetHand()->GetHotOilCoolDown();
		CoolDownMax = CGamePlayState::GetInstance()->GetHand()->GetHotOilCoolDownMax();
		ButtonLevel = 9001;
	}
	else if(GetButtonType() == BUTTON_ARCHER)
	{
		CurrCoolDown = CGamePlayState::GetInstance()->GetHand()->GetArcherCoolDown();
		CoolDownMax = CGamePlayState::GetInstance()->GetHand()->GetArcherCoolDownMax();
		ButtonLevel = CGamePlayState::GetInstance()->GetHand()->GetArcherLevel();
	}

	if(ButtonLevel < 0)
		ButtonLevel = 1;
	if(GetButtonType() != BUTTON_CASTLEUPGRADE)
	{
		pTM->Draw(Bars, ButtonRect.left + 10, ButtonRect.bottom, (float)(ButtonRect.right - ButtonRect.left - 20), 10.0f, 0,0,0,0, D3DCOLOR_ARGB(255,0,255,0));
		if(CurrCoolDown >= 0)
			pTM->Draw(Bars, ButtonRect.left + 10, ButtonRect.bottom, (float)(CurrCoolDown/CoolDownMax *  (float)(ButtonRect.right - ButtonRect.left - 20)), 10.0f, 0,0,0,0, D3DCOLOR_ARGB(255,100,128,255));

		char Level[32];
		sprintf_s( Level,"LVL: %d", ButtonLevel);
		if(ButtonLevel == 9001)
			pBF->Render("", ButtonRect.left + 5, ButtonRect.bottom + 10, .75f, 0.50f, D3DCOLOR_ARGB(255, 0,0,0), MENUFONT);
		else if((GetButtonType() == BUTTON_ARCHER && ButtonLevel >= 10) || (GetButtonType() == BUTTON_ROCK && ButtonLevel >= 5))
			pBF->Render("LVL: MAX!!", ButtonRect.left, ButtonRect.bottom + 10, .5f, 0.50f, D3DCOLOR_ARGB(255, 0,0,0), MENUFONT);		
		else
			pBF->Render(Level, ButtonRect.left + 5, ButtonRect.bottom + 10, .75f, 0.50f, D3DCOLOR_ARGB(255, 0,0,0), MENUFONT);
	}
	//CSGD_Direct3D::GetInstance()->DrawRect(ButtonRect,0,0,0);
}

void CButton::Update( float fElapsedTime )
{
	timer -= fElapsedTime;
}

void CButton::SetUpRect(int Left, int Top, int Right, int Bottom)
{
	SetRect(&ButtonRect, Left, Top, Right, Bottom); 
}
void CButton::SetButtonColor(int A, int R, int G, int B)
{
	ButtonColor.A = A;
	ButtonColor.R = R;
	ButtonColor.G = G;
	ButtonColor.B = B;
}

bool CButton::CheckCollision( IBaseInterface* pBase )
{
	RECT TempRect;

	if(pBase->GetType() == OBJ_HAND)
	{
		CMasterHand* hand = (CMasterHand*)pBase;

		if(!hand->GetHoldingEnemy())
		{
			if(IntersectRect(&TempRect, &ButtonRect, &hand->GetCursorBox() ) )
			{
				ShowPrice = true;

				if(timer < 0)
				{
					timer = 0.1f;

					if(InputManager::GetInstance()->Action())
					{
						if(ButtonType == BUTTON_HAND)
						{
							CGamePlayState::GetInstance()->GetHand()->EquiptPinch();
						}
						if(ButtonType == BUTTON_ROCK)
						{
							if(CGamePlayState::GetInstance()->GetPlayer()->GetCash() >= GetPrice())
								CGamePlayState::GetInstance()->GetHand()->EquiptRock();
						}
						if(ButtonType == BUTTON_POISON)
						{
							CGamePlayState::GetInstance()->GetHand()->EquiptPoison();
						}
						if(ButtonType == BUTTON_OIL)
						{
							if(CGamePlayState::GetInstance()->GetHand()->GetHotOilCoolDown() >= CGamePlayState::GetInstance()->GetHand()->GetHotOilCoolDownMax())
							{
								if(CGamePlayState::GetInstance()->GetPlayer()->GetCash() >= GetPrice())
								{
									CGamePlayState::GetInstance()->GetHand()->PourOil();
									CGamePlayState::GetInstance()->GetPlayer()->SetCash(CGamePlayState::GetInstance()->GetPlayer()->GetCash() - GetPrice());
								}
							}
						}
						if(ButtonType == BUTTON_BALLISTA)
						{
							if(CGamePlayState::GetInstance()->GetHand()->GetBallistaCoolDown() >= CGamePlayState::GetInstance()->GetHand()->GetBallistaCoolDownMax())
							{
								if(CGamePlayState::GetInstance()->GetPlayer()->GetCash() >= GetPrice())
								{
									CGamePlayState::GetInstance()->GetHand()->FireBallista();
									CGamePlayState::GetInstance()->GetPlayer()->SetCash(CGamePlayState::GetInstance()->GetPlayer()->GetCash() - GetPrice());

									
									MessageSystem::GetInstance()->SendNewMessage(new CCreateProjectileMessage(300.0f, -70.0f, 0.0f, 50.0f, 3));
								}
							}
						}
						if(ButtonType == BUTTON_ARCHER)
						{
							if(CGamePlayState::GetInstance()->GetPlayer()->GetCash() >= GetPrice())
							{
								if(CGamePlayState::GetInstance()->GetHand()->GetArcherCoolDown() >= CGamePlayState::GetInstance()->GetHand()->GetArcherCoolDownMax())
								{
									if(CGamePlayState::GetInstance()->GetHand()->GetArcherLevel() < 10)
									{
										CGamePlayState::GetInstance()->GetHand()->BuyArcher();
										CGamePlayState::GetInstance()->GetPlayer()->SetCash(CGamePlayState::GetInstance()->GetPlayer()->GetCash() - GetPrice());
										SetPrice(100 + (50 * CGamePlayState::GetInstance()->GetHand()->GetArcherLevel()));
									}
								}
							}
						}
						if(ButtonType == BUTTON_CASTLEUPGRADE)
						{
							
								if(CGamePlayState::GetInstance()->GetPlayer()->GetCash() >= GetPrice() && CGamePlayState::GetInstance()->GetPlayer()->GetCastleLevel() < 3)
								{
									CGamePlayState::GetInstance()->GetPlayer()->UpgradeCastle();
									CGamePlayState::GetInstance()->GetPlayer()->SetCash(CGamePlayState::GetInstance()->GetPlayer()->GetCash() - GetPrice());
									if(CGamePlayState::GetInstance()->GetPlayer()->GetCastleLevel() == 2)
										SetPrice(5000);
								}
						}
					}
					else if(InputManager::GetInstance()->Upgrade())
					{
						if(CGamePlayState::GetInstance()->GetHand()->GetRockLevel() < 5)
						{
							if(ButtonType == BUTTON_ROCK && CGamePlayState::GetInstance()->GetPlayer()->GetCash() >= (GetPrice()* 2) )
							{
								CGamePlayState::GetInstance()->GetPlayer()->SetCash((CGamePlayState::GetInstance()->GetPlayer()->GetCash()) - (GetPrice() * 2));
								CGamePlayState::GetInstance()->GetHand()->RockLevelUp();
								SetPrice(250 * CGamePlayState::GetInstance()->GetHand()->GetRockLevel());
							}
						}
					}
				}
				return true;
			}		

			else
				ShowPrice = false;
		}
	}


	if( pBase->GetType() == OBJ_PLAYER )
	{	
		CPlayer* pPBase = (CPlayer*)pBase;
		return true;
	}

	return false;
}